/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SearchView.java
 *
 * Created on 2-Apr-2010, 6:09:26 PM
 */

package ticketmaster.presentation.customer.purchase;

import com.michaelbaranov.microba.calendar.DatePicker;
import java.sql.Timestamp;
import java.util.Collection;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import ticketmaster.businesslogic.SearchController;
import ticketmaster.dataobject.EventView;
import ticketmaster.presentation.customer.CustomerBasePanel;
import ticketmaster.util.DateTimeHelper;

/**
 *
 * @author Xiaolong Xue
 */
public class SearchView extends CustomerBasePanel
{

    /** Creates new form SearchView */
    public SearchView()
    {
        initComponents();
        initDatePicker();
        addDataToComboBox();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblChooseEvent = new javax.swing.JLabel();
        comboEvent = new javax.swing.JComboBox();
        lblFrom = new javax.swing.JLabel();
        lblTo = new javax.swing.JLabel();
        btnSearch = new javax.swing.JButton();
        btnAdvSearch = new javax.swing.JButton();
        lbTitle = new javax.swing.JLabel();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ticketmaster.TicketMasterApp.class).getContext().getResourceMap(SearchView.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblChooseEvent.setFont(resourceMap.getFont("lblChooseEvent.font")); // NOI18N
        lblChooseEvent.setText(resourceMap.getString("lblChooseEvent.text")); // NOI18N
        lblChooseEvent.setName("lblChooseEvent"); // NOI18N
        add(lblChooseEvent, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 130, 109, 37));

        comboEvent.setFont(resourceMap.getFont("comboEvent.font")); // NOI18N
        comboEvent.setMaximumRowCount(60);
        comboEvent.setName("comboEvent"); // NOI18N
        add(comboEvent, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 120, 410, 60));

        lblFrom.setFont(resourceMap.getFont("lblFrom.font")); // NOI18N
        lblFrom.setText(resourceMap.getString("lblFrom.text")); // NOI18N
        lblFrom.setName("lblFrom"); // NOI18N
        add(lblFrom, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 230, 50, 34));

        lblTo.setFont(resourceMap.getFont("lblTo.font")); // NOI18N
        lblTo.setText(resourceMap.getString("lblTo.text")); // NOI18N
        lblTo.setName("lblTo"); // NOI18N
        add(lblTo, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 330, 30, 30));

        btnSearch.setFont(resourceMap.getFont("btnSearch.font")); // NOI18N
        btnSearch.setText(resourceMap.getString("btnSearch.text")); // NOI18N
        btnSearch.setName("btnSearch"); // NOI18N
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });
        add(btnSearch, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 440, 110, 60));

        btnAdvSearch.setFont(resourceMap.getFont("btnAdvSearch.font")); // NOI18N
        btnAdvSearch.setText(resourceMap.getString("btnAdvSearch.text")); // NOI18N
        btnAdvSearch.setName("btnAdvSearch"); // NOI18N
        btnAdvSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdvSearchActionPerformed(evt);
            }
        });
        add(btnAdvSearch, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 440, 170, 60));

        lbTitle.setFont(resourceMap.getFont("lbTitle.font")); // NOI18N
        lbTitle.setText(resourceMap.getString("lbTitle.text")); // NOI18N
        lbTitle.setName("lbTitle"); // NOI18N
        add(lbTitle, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 25, -1, -1));
    }// </editor-fold>//GEN-END:initComponents

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        searchEvent();
    }//GEN-LAST:event_btnSearchActionPerformed

    private void btnAdvSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdvSearchActionPerformed
        // TODO add your handling code here:
        AdvanceSearchView asv = new AdvanceSearchView();

        this.getPnlParent().removeAll();
        this.getPnlParent().add(asv);
        this.getPnlParent().validate();
        this.getPnlParent().repaint();
        asv.setPnlParent(getPnlParent());
    }//GEN-LAST:event_btnAdvSearchActionPerformed

    /**
    * @param args the command line arguments
    */
    /*
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new SearchView().setVisible(true);
            }
        });
    }
    */
    // add value into combo box
    private void addDataToComboBox()
    {
        /*
        ArrayList<Event> eventDO = (ArrayList<Event>)new EventDAO().getEventsDO(new Event());
        int eventNumber = eventDO.size();
        String[] eventName = new String[eventNumber];
        for(int i = 0; i < eventNumber; ++i)
            eventName[i] = eventDO.get(i).getName();
        

        Collection<Event> events = DAOFactory.getInstance().getEventDAO().getEventsDO(new Event());

        Iterator<Event> eventIterator = events.iterator();

        while(eventIterator.hasNext())
            comboEvent.addItem(eventIterator.next().getName());*/
        String[] events = {"Ice Hockey - Men","Ice Hockey - Women","Figure Skating","Curling"};
        for(int i = 0; i < events.length; ++i)
            comboEvent.addItem(events[i]);
    }

    // search event
    private void  searchEvent()
    {
        String keyword = (String)comboEvent.getSelectedItem();

        Timestamp startDateTS = DateTimeHelper.DateToTimestamp(startDate.getDate());
        Timestamp endDateTS = DateTimeHelper.DateToTimestamp(endDate.getDate());

        if(startDateTS.after(endDateTS))
        {
            JOptionPane.showMessageDialog(null, "start date cannot be after "
                    + "end date", "Search Event", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        EventView event = new EventView();
        event.setName(keyword);
        event.setBeginDateTime(startDateTS);
        event.setEndDateTime(endDateTS);

        Collection<EventView> events = null;
        SearchController controller = new SearchController();
        events = controller.simpleSearch(event);
        
        SearchResultsView srv = new SearchResultsView(events);
        this.getPnlParent().removeAll();
        this.getPnlParent().add(srv);
        this.getPnlParent().validate();
        this.getPnlParent().repaint();
        srv.setPnlParent(this.getPnlParent());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdvSearch;
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox comboEvent;
    private javax.swing.JLabel lbTitle;
    private javax.swing.JLabel lblChooseEvent;
    private javax.swing.JLabel lblFrom;
    private javax.swing.JLabel lblTo;
    // End of variables declaration//GEN-END:variables
    private DatePicker startDate;
    private DatePicker endDate;
    
    private void initDatePicker()
    {
        EmptyBorder border = new EmptyBorder(new Insets(10, 20, 10, 20));

        startDate = new DatePicker();

        JFormattedTextField startTextField = (JFormattedTextField) startDate.getComponent(0);
        startTextField.setFont(SearchView.getCustomFont());

        JButton startDateButton = (JButton) startDate.getComponent(1);
        startDateButton.setBorder(border);

        add(startDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 230, 350, 60));

        endDate = new DatePicker();

        JFormattedTextField endTextField = (JFormattedTextField) endDate.getComponent(0);
        endTextField.setFont(SearchView.getCustomFont());

        endDate.setFont(SearchView.getCustomFont());
        JButton endDateButton = (JButton) endDate.getComponent(1);
        endDateButton.setBorder(border);

        add(endDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 330, 350, 60));
    }

}
